package com.qf.news.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.news.entity.NewsDetail;
import com.qf.news.mapper.NewsDetailMapper;
import com.qf.news.service.NewsDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class NewsDetailServiceImpl implements NewsDetailService {

    @Autowired
    private NewsDetailMapper newsDetailMapper;


    @Override
    @Transactional(rollbackFor = Throwable.class)
    public void deleteById(Long id) {
        int i = newsDetailMapper.deleteByPrimaryKey(id);
        if(i!=1){
            System.out.println("【删除新闻信息】删除失败！");
            throw new RuntimeException("【删除新闻信息】删除失败！");
        }
    }

    @Override
    public PageInfo<NewsDetail> findAllByPage(Integer page, Integer rows) {
        //1、分页
        PageHelper.startPage(page, rows);

        //2、查询
        List<NewsDetail> newsList=newsDetailMapper.findByTitle(null);

        //3、封装PageInfo
        PageInfo<NewsDetail> pageInfo = new PageInfo<>(newsList);

        //4、返回
        return pageInfo;
    }

    @Override
    public PageInfo<NewsDetail> findByTitleAndPage(Integer page, Integer rows, String title) {
        //1、分页
        PageHelper.startPage(page, rows);

        //2、查询
        List<NewsDetail> newsList=newsDetailMapper.findByTitle(title);

        //3、封装PageInfo
        PageInfo<NewsDetail> pageInfo = new PageInfo<>(newsList);

        //4、返回
        return pageInfo;
    }
}
